/**
 * 定义页面对应的后台处理类完整路径。
 */
var clsUrl = basePath + 'JarpListener?className=cn.com.zsc.cm.IntegrateCost';
var status;

Ext.onReady(function(){
	Ext.QuickTips.init();
	
	/**
	 * 费用分类数据源
	 */
	var feeTypeStore=new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : comboBoxUrl + '29'
		}),
		reader : new Ext.data.JsonReader({
			fields : [
			{name:'CL_CODE'},
			{name:'CL_NAME'}
		]
		}),
		autoLoad : true
	});
	
	/**
	 * 费用分类控件
	 */
	var feeType = new Ext.form.ComboBox({
		fieldLabel : '费用分类',
		id : 'feeType',
		width : 100,
		displayField : 'CL_NAME',
		valueField : 'CL_CODE',
		emptyText : '请选择',
		store:feeTypeStore,
		mode : 'local',
		editable  : false,
		triggerAction : 'all',
		selectOnFocus : true
	});
	
	/**
	 * 成本分类数据源
	 */
	var costTypeStore=new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : comboBoxUrl + '28'
		}),
		reader : new Ext.data.JsonReader({
			fields : [
			{name:'CL_CODE'},
			{name:'CL_NAME'}
		]
		}),
		autoLoad : true
	});
	
	/**
	 * 成本分类控件
	 */
	var costType = new Ext.form.ComboBox({
		fieldLabel : '成本分类',
		id : 'costType',
		width : 100,
		displayField : 'CL_NAME',
		valueField : 'CL_CODE',
		emptyText : '请选择',
		store:costTypeStore,
		mode : 'local',
		editable  : false,
		triggerAction : 'all',
		selectOnFocus : true
	});
	
	/**
	 * 发生时间开始控件
	 */
	var startDate = new Ext.form.DateField({
		id:'startDate',
        fieldLabel:"费用发生时间",
        name:'startDate',
        format:'Y-m-d',
        editable:false,
        width:100
    });
    
    /**
	 * 发生时间结束控件
	 */
	var endDate = new Ext.form.DateField({
		id:'endDate',
        fieldLabel:"至",
        name:'endDate',
        format:'Y-m-d',
        editable:false,
        width:100
    });
    
    /**
     * 金额下限
     */
    var startAmount = new Ext.form.NumberField({fieldLabel: '金额',width:100,name:'startAmount',id:'startAmount',xtype:'numberfield',allowNegative:false})
    
    /**
     * 金额上限
     */
    var endAmount = new Ext.form.NumberField({fieldLabel: '至',width:100,name:'endAmount',id:'endAmount',xtype:'numberfield',allowNegative:false})
    
    /**
     * 费用名称
     */
    var cName = new Ext.form.TextField({fieldLabel: '费用名称',width:150,name:'cName',id:'cName',xtype:'textfield'})
    
    /**
     * 录入人
     */
    var ep = new Ext.form.TextField({fieldLabel: '录入人',width:150,name:'ep',id:'ep',xtype:'textfield'})
    
	var storereader= new Ext.data.JsonReader({
    	fields:[
    		{name :'ID'},
    		{name :'CNAME'},
    		{name :'CTYPE'},
    		{name :'TYPE'},
    		{name :'CTYPE'},
			{name :'AMOUNT'},
			{name :'DATE'},
			{name :'EP'},
			{name :'REMARKED'},
			
			{name:'SHOW_TYPE'},
			{name:'SHOW_CTYPE'},
			{name:'SHOW_EP'}
    	]
	});
		
	/**
	 * 查询分页数据
	 */
	var store = new Ext.data.Store({
		reader:storereader,
		proxy:new Ext.data.HttpProxy({
			url:clsUrl+'&methodName=getCost&paramCount=0'
		})
	});
     
    var searchPanel = new Ext.Panel({
		id:"searchPanel",
		region:"north",
		border:false,
		layout : 'column',
		labelAlign : 'right',
		height:80,
		frame:true,
		labelWidth : 80,
		items:[{
			layout : 'form',
			border:false,
		  	labelWidth : 80,
		  	labelAlign : 'right',
		  	columnWidth : .19,
		  	items:[startDate,startAmount]
		},{
			layout : 'form',
		  	labelWidth : 10,
		  	labelAlign : 'right',
		  	columnWidth : .20,
		  	items:[endDate,endAmount]
		},{
			layout : 'form',
		  	labelWidth : 60,
		  	labelAlign : 'right',
		  	columnWidth : .20,
		  	items:[costType,feeType]
		},{
			layout : 'form',
		  	labelWidth : 60,
		  	labelAlign : 'right',
		  	columnWidth : .25,
		  	items:[cName,ep]
		},{
			layout : 'form',
		  	labelWidth : 60,
		  	labelAlign : 'right',
		  	columnWidth : .16,
		  	items:[{
	  			layout : 'column',
				labelAlign : 'right',
				items:[{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .5,
				  	items:[
				  		{xtype:'button',width:70,text:'查询',handler:search},
				  		{height:4},
				  		{xtype:'button',width:70,text:'导出Excel',handler:exportExcel}
				  	]
				},{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .5,
				  	items:[
				  		{xtype:'button',width:70,text:'重置',handler:function() {
					  			Ext.getCmp("startDate").setValue("");
					  			Ext.getCmp("endDate").setValue("");
					  			Ext.getCmp("startAmount").setValue("");
					  			Ext.getCmp("endAmount").setValue("");
					  			Ext.getCmp("feeType").setValue("");
					  			Ext.getCmp("costType").setValue("");
					  			Ext.getCmp("cName").setValue("");
					  			Ext.getCmp("ep").setValue("");
							}
						}
				    ]
				}]
			}]
		}],
		keys: {
            key: 13,
            fn: search
        }
	});
                
	/**
	 * 用户列表
	 */
	var basegrid = new Ext.grid.GridPanel( {
		id :'basegrid',
		store :store,
		split :true,
		bodyStyle:'5px 15px 0',
		region :'center',
		border :false,
		enableColumnMove:false,                                           
	  	enableHdMenu:false,
		frame :false,
		autoScroll :true,
		viewConfig: {
        	forceFit: true
    	},
    	columns: [
     	            new Ext.grid.RowNumberer(),
    	          	{id:'cName',name:'cName', header:'费用名称',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'CNAME'},
    	          	{header:'成本类别',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'SHOW_CTYPE'},
    	          	{header:'费用分类',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'SHOW_TYPE'},
    	          	{id:'type',name:'type',header:'费用分类',sortable:true,hidden:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'TYPE'},
    	          	{id:'amount',name:'amount',header:'金额',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false,dataIndex: 'AMOUNT'},
    	          	{id:'date',name:'date',header:'发生时间',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false,dataIndex: 'DATE'},
    	          	{id:'ep',name:'ep',header:'录入人',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false,dataIndex: 'SHOW_EP'},
    	          	{id:'ctype',name:'ctype',header:'成本类别',sortable:true,hidden:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'CTYPE'},
    	          	{id:'remarked',name:'remarked',header:'备注',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'REMARKED'}
    	]
	});
	
	var mainPanel = new Ext.Panel({
            id: 'mainPanel',
            region:'center',
            split:true,
            border:false,
            frame:true,
            layout: 'border',  
            items:[searchPanel,basegrid]
    }); 
    
	var viewport = new Ext.Viewport( {
		layout:'border',
		items: [mainPanel]
	});
	
	function search(){
		if(Ext.getCmp("startDate").getValue() > Ext.getCmp("endDate").getValue()) {
			Ext.getCmp("startDate").markInvalid("费用发生时间开始必须小于费用发生时间结束");
			return false;
		}
		
		mainPanel.body.mask('正在加载数据...','x-mask-loading');
		var obj = new Object();
		obj.startDate = Ext.getCmp("startDate").getValue();
		obj.endDate = Ext.getCmp("endDate").getValue();
		obj.startAmount = Ext.getCmp("startAmount").getValue();
		obj.endAmount = Ext.getCmp("endAmount").getValue();
		obj.feeType = Ext.getCmp("feeType").getValue();
		obj.costType = Ext.getCmp("costType").getValue();
		obj.cName = Ext.getCmp("cName").getValue();
		obj.ep = Ext.getCmp("ep").getValue();
		
    	var url=clsUrl+'&methodName=getCost&paramCount=0'
		Ext.http.send(url,function(res){
			basegrid.store.loadData(Ext.decode(res));
			mainPanel.body.unmask();
		},Ext.encode(obj));
	}
	
	function exportExcel() {
		var vExportContent = basegrid.getExcelXml(); // 获取数据
		if (Ext.isIE6 || Ext.isIE7 || Ext.isIE8 || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3) { // 判断浏览器
			var fd = Ext.get('frmDummy');
			if (!fd) {
				fd = Ext.DomHelper.append(Ext.getBody(), {
							tag : 'form',
							method : 'post',
							id : 'frmDummy',
							action : exportUrl,
							target : '_blank',
							name : 'frmDummy',
							cls : 'x-hidden',
							cn : [{
										tag : 'input',
										name : 'exportContent',
										id : 'exportContent',
										type : 'hidden'
									}]
						}, true);
			}
			fd.child('#exportContent').set({
						value : vExportContent
					});
			fd.dom.submit();
		} else {
			document.location = 'data:application/vnd.ms-excel;base64,'+ Base64.encode(vExportContent);
		}
	}
	
	store.on('beforeload',function(thiz,options){
		mainPanel.body.mask('正在加载数据','x-mask-loading');
    });
    
	store.on("load",function(){
		mainPanel.body.unmask();
	});
	
	store.load();
});